<?php
	
	namespace Aspic\Form\Element;
	use Aspic as A;
	use Aspic\Security\Security as S;
	
	/**
	* ComboBox form element
	*/
	class ComboBox extends AbstractStandardElement {
		const DEFAULT_GROUP_ID = 'defaultGroupId';
		
		protected $_itemsGroups; // Organized by group
		
		public function __construct($name, $label = '', array $items = array(), array $options = array(), array $attributes = array(), array $validations = array()) {
			parent::__construct($name, $label, $options, $attributes, $validations);
			
			$this->_itemsGroups = array();
			
			$this->addItemsGroup(self::DEFAULT_GROUP_ID, '', '');
			
			$this->setItems($items);
		}
		
		public function setItems($items) {
			foreach($items as $item) {
				$item['value'] = A\Util::ifSet($item['value'], $item['label']);
			
				$item = array_merge(
					array(
						'label' => '',
						'value' => '',
						'selected' => false,
						'group' => null, 
						'attributes' => array(), 
					),
					$item
				);
				
				$this->addItem($item['label'], $item['value'], $item['selected'], $item['attributes'], $item['group']);
			}
		}
		
		/**
		* Set items from a 1-dimension array like: array('value1' => 'label1', 'value2' => 'label2')
		*/
		public function setKeyValueItems($items) {
			$parsedItems = array();
			
			foreach($items as $value => $label) {
				$item = array();
				
				$item['value'] = $value;
				$item['label'] = $label;
				
				$parsedItems[] = $item;
			}

			return $this->setItems($parsedItems);
		}
		
		public function addItem($label, $value = null, $selected = false, array $attributes, $group = null) {
			$value = \Aspic\Util::ifSet($value, $label);
			
			if (!$selected) {
				$selected = null;
			}
			
			$item = array(
				'label' => $label,
				'attributes' => array_merge( 
					$attributes, 
					array(
						'value' => $value, 
						'selected' => $selected
					)
				)
			);
			
			if(is_null($group) OR !isset($this->_itemsGroups[$group])) {
				$group = self::DEFAULT_GROUP_ID;
			}
			
			$this->_itemsGroups[$group]['items'][] = $item;
			
			return $this;
		}
		
		/**
		* Set one or many (array) item to set selected
		*/
		public function setSelectedItem($itemValues) {
			if(!is_array($itemValues)) {
				$itemValues = array($itemValues);
			}
		
			foreach($this->_itemsGroups as $k1 => $ig) {
				foreach($ig['items'] as $k2 => $i) {
					if(in_array($i['attributes']['value'], $itemValues)) {
						$this->_itemsGroups[$k1]['items'][$k2]['attributes']['selected'] = 'selected';
					}
					else {
						$this->_itemsGroups[$k1]['items'][$k2]['attributes']['selected'] = null;
					}					
				}
			}
		}
		
		/**
		* Add a 'optgroup' (html tag) to the selectbox
		* 
		* @param string $name An id for the group
		*/
		public function addItemsGroup($name, $label, $class = '', array $attributes = array()) {
			$this->_itemsGroups[$name] = array(
				'name' => $name,
				'attributes' => array_merge(
					array(
						'class' => $class, 
						'label' => $label
					), 
					$attributes), 
				'items' => array()
			);
			
			return $this;
		}
		
		public function outInput() {
			$out = '<select '.$this->getAttrsAndNameStr().'>';
		
			foreach($this->_itemsGroups as $itemsGroupId => $itemsGroup) {
				if($itemsGroupId != self::DEFAULT_GROUP_ID) {
					$out .= '<optgroup'.A\UString::implodeHtmlAttributes($itemsGroup['attributes']).'>';
					
					$out .= $this->getGroupOptionsString($itemsGroup);
					
					$out .= '</optgroup>';
				}
				else {
					$out .= $this->getGroupOptionsString($itemsGroup);
				}
			
				
			}
			
			$out .= '</select>';
			
			return $out;
		}
		
		protected function getGroupOptionsString($arrGroup) {
			$out = '';
		
			foreach($arrGroup['items'] as $item) {
//				if(!A\Util::getIfSet($item['attributes']['selected'])) {
//					unset($item['attributes']['selected']);
//				}
//				else {
//					$item['attributes']['selected'] = '1';
//				}
				
				$out .= '<option'.\Aspic\UString::implodeHtmlAttributes($item['attributes']).'>'.$item['label'].'</option>';
			}
			
			return $out;
		}
		
		public function bind(array $formData = null) {
			if($value = $this->getValue($formData)) { // $value could be an array here (attr multiple)
				foreach($this->_itemsGroups as $itemsGroupId => $itemsGroup) {
					foreach($itemsGroup['items'] as $key => $it) {
						if(A\Util::egalOrInArray($it['attributes']['value'], $value)) {
							$this->_itemsGroups[$itemsGroupId]['items'][$key]['attributes']['selected'] = true;
						}
						else {
							$this->_itemsGroups[$itemsGroupId]['items'][$key]['attributes']['selected'] = null;
						}
					}
				}
			}
		}
		
		protected function getDefaultId() {
			return 'select'.ucfirst($this->getName());
		}

		
	}	
	
?>